package cn.edu.ctbu.demo2.webapi;


import cn.edu.ctbu.demo2.core.PageUntils;
import cn.edu.ctbu.demo2.domain.Score;
import cn.edu.ctbu.demo2.domain.Student;
import cn.edu.ctbu.demo2.service.ScoreService;
import cn.edu.ctbu.demo2.service.StudentService;
import cn.edu.ctbu.demo2.utils.RUtil;
import cn.edu.ctbu.demo2.vo.R;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.util.StringUtils;

@RestController
@Slf4j
@RequestMapping("/webapi/score")
public class ScoreRestController {
    @Autowired
    private ScoreService scoreService;

    @PostMapping("/insert")
    public Score insert(Score score){
        Score score1=scoreService.Insert(score);
        return score1;
    }

    @PostMapping("/add")
    public R add(@Valid Score score, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            String errorMsg=bindingResult.getFieldError().getDefaultMessage();
            log.info("新增成绩出错了!{}",errorMsg);
            return RUtil.error(-10,errorMsg);
        }
        scoreService.Insert(score);
        return RUtil.success();
    }

    @GetMapping("/getbypage")
    public PageUntils getByPage(@RequestParam(value = "page", defaultValue = "0") Integer page,
                                @RequestParam(value = "size", defaultValue = "10") Integer size,
                                @RequestParam(value = "stu_no",defaultValue = "")String stu_no){

        /**
         * 按id排序
         */
        Sort sort=Sort.by(Sort.Direction.DESC,"id");

        Page<Score> scorePage;
        if(StringUtils.isEmpty(stu_no)){
            Pageable pageable= PageRequest.of(page,size,sort);
            scorePage = scoreService.findAll(pageable);
        }else{
            Score score=new Score();
            score.setStu_no(stu_no);
            Pageable pageable=PageRequest.of(0,10,sort);
            ExampleMatcher matcher=ExampleMatcher.matching()
                    .withMatcher("name",ExampleMatcher.GenericPropertyMatchers.contains());
            Example<Score> example = Example.of(score,matcher);
            scorePage = scoreService.findAll(example,pageable);
        }
        PageUntils pageUntils=new PageUntils(scorePage.getContent(),scorePage.getTotalElements());
        return pageUntils;
    }

    @GetMapping("/get/{id}")
    public Score get(@PathVariable Integer id){
        Score score=scoreService.getById(id);
//        score.setPassword("");
        return score;
    }

    @PutMapping("/update")
    public Score update(Score score){
        //读取旧数据
        Score oldscore=scoreService.getById(score.getId());
//        if(StringUtils.isEmpty(score.getPassword())){
            //没有新密码，那么就是旧密码
//            score.setPassword(oldstudent.getPassword());
//        }
        score=scoreService.update(score);
        return score;
    }

    @DeleteMapping("/delete/{id}")
    public void  delete(@PathVariable Integer id){
        scoreService.delete(id);
//        return student1;
    }
}
